<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 试题 </el-breadcrumb-item>
        <el-breadcrumb-item>题目表 </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        题目：<el-input v-model="query.title"
                  placeholder="题目"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>

        单选/多选：<el-input v-model="query.type"
                  placeholder="单选/多选"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>

        <el-dropdown @command="getText">
          <span class="el-dropdown-link">类型 <i class="el-icon-arrow-down el-icon--right"></i> </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="item in dropdownData"
                              :key="item.id"
                              :command="item">
              {{ item.categoryname }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-input placeholder="类型"
                  class="handle-input mr10"
                  style="width: 100px"
                  v-model="category_name"
                  readonly=""></el-input>
        <el-input placeholder="id"
                  class="handle-input mr10"
                  style="width: 60px"
                  v-model="category_id"
                  readonly=""></el-input>
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleSearch"
                   style="width: 100px; margin-left: 100px">搜索</el-button>
      </div>

      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                :row-key="getRowKeys">
        <el-table-column type="selection"
                         width="55"
                         align="center"
                         :reserve-selection="true"></el-table-column>
        <el-table-column prop="id"
                         label="ID"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="title"
                         label="题目"></el-table-column>
        <el-table-column prop="optionA"
                         label="选项A"> </el-table-column>
        <el-table-column prop="optionB"
                         label="选项B"></el-table-column>
        <el-table-column prop="optionC"
                         label="选项C"></el-table-column>
        <el-table-column prop="optionD"
                         label="选项D"></el-table-column>
        <el-table-column prop="answer"
                         label="答案"></el-table-column>
        <el-table-column prop="categoryid"
                         label="类型"> </el-table-column>
        <el-table-column prop="type"
                         label="单选/多选"></el-table-column>

        <el-table-column label="操作"
                         width="200"
                         align="center">
          <template slot-scope="scope">
            <el-button type="text"
                       icon="el-icon-edit"
                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text"
                       icon="el-icon-delete"
                       class="red"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <div style="float: left">
          <div>
            <el-button type="danger"
                       icon="el-icon-delete"
                       class="handle-del mr10"
                       @click="delAllSelection">批量删除</el-button>

            <el-button @click="add"
                       style="width: 100px; margin-left: 430px; background-color: #409eff; color: white">新增</el-button>
          </div>
        </div>
        <!-- 分页组件 -->
        <Pagination v-bind:child-msg="pageparm"
                    @callFather="callFather"></Pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑"
               :visible.sync="editVisible"
               width="30%">
      <el-form ref="form"
               :model="form"
               label-width="70px">
        <el-form-item label="id">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="题目">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="选项A">
          <el-input v-model="form.optionA"></el-input>
        </el-form-item>
        <el-form-item label="选项B">
          <el-input v-model="form.optionB"></el-input>
        </el-form-item>
        <el-form-item label="选项C">
          <el-input v-model="form.optionC"></el-input>
        </el-form-item>
        <el-form-item label="选项D">
          <el-input v-model="form.optionD"></el-input>
        </el-form-item>
        <el-form-item label="答案">
          <el-input v-model="form.answer"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="form.categoryid"></el-input>
        </el-form-item>
        <!-- <el-form-item>
                <el-dropdown @command="getText1">
                    <span class="el-dropdown-link">类型 <i class="el-icon-arrow-down el-icon--right"></i> </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-for="item in dropdownData" :key="item.id" :command="item">
                            {{ item.categoryname }}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-input placeholder="类型" class="handle-input mr10" style="width: 100px" v-model="category_id1"></el-input>
</el-form-item> -->
        <el-form-item label="单选/多选">
          <el-input v-model="form.type"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/common/Pagination.vue';
export default {
    name: 'titletable',
    data() {
        return {
            category_name: '',
            category_id: '',
            query: {
                id: '',
                title: '',
                optionA: '',
                optionB: '',
                optionC: '',
                optionD: '',
                answer: '',
                type: '',
                categoryid: ''
            },
            formInline: {
                page: 1,
                limit: 5
            },
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 5,
                total: 5
            },
            dropdownData: [],
            tableData: [],
            current: '',
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    // 注册组件
    components: {
        Pagination
    },
    created() {
        this.getData(this.formInline);
    },

    methods: {
        add() {
            this.$router.push('/createTitle');
        },

        getText(command) {
            this.category_name = command.categoryname;
            this.category_id = command.id;
        },

        // 获取题目表
        getData(parameter) {
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;

            this.$axios
                .post('/api/title/findTitle/' + this.pageparm.currentPage + '/' + this.pageparm.pageSize, this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.data.list;
                    this.pageparm.total = res.data.data.total;
                })
                .catch((err) => {
                    console.log(err);
                });
            //渲染下拉框
            this.$axios
                .post('/api/category/findCategory/', {})
                .then((res) => {
                    console.log(res);
                    this.dropdownData = res.data.data;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 分页插件事件
        callFather(parm) {
            //子传父 当前页和每页大小
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getData(this.formInline);
        },
        getRowKeys(row) {
            return row.id;
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.pageparm, 'currentPage', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios
                        .delete('/api/title/deleteTitle/' + row.id)
                        .then((res) => {
                            if (res.data.code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: '数据已删除!'
                                });
                                //重新获取当前页面数据重新渲染列表！
                                this.getData(this.pageparm);
                            } else {
                                this.$message({
                                    type: 'info'
                                    // message: res.data.msg
                                });
                            }
                        })
                        .catch((err) => {
                            //this.$message.error('数据删除失败，请稍后再试！')
                            console.log(err);
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            //console.log(val);
            this.multipleSelection = val;
        },
        delAllSelection() {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].trainingname + ' ';
                        console.log(this.multipleSelection[i].id);
                        this.$axios
                            .delete('/api/title/deleteTitle/' + this.multipleSelection[i].id)
                            .then((res) => {
                                if (res.data.code == 200) {
                                    this.$message({
                                        type: 'success',
                                        message: '数据已删除!'
                                    });
                                    //重新获取当前页面数据重新渲染列表！
                                    this.getData(this.pageparm);
                                } else {
                                    this.$message({
                                        type: 'info'
                                        // message: res.data.msg
                                    });
                                }
                            })
                            .catch((err) => {
                                this.$message.error('数据删除失败，请稍后再试！');
                                console.log(err);
                            });
                        //console.log(str);
                    }
                    // this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },

        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            //this.$set(this.tableData, this.idx, this.form);
            this.$axios
                .post('/api/title/updateTitle/', this.form)
                .then((res) => {
                    if (res.data.code == 200) {
                        this.getData(this.pageparm);
                        this.$message({
                            type: 'success',
                            message: '数据修改成功！'
                        });
                        this.editFormVisible = false;
                    } else {
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                        this.editFormVisible = false;
                    }
                })
                .catch();
        }
    }
};
</script>

<style scoped>
.el-dropdown-link {
    cursor: pointer;
    color: #409eff;
}
.el-icon-arrow-down {
    font-size: 12px;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
